package views.mediators
{
	import com.axon.base.helpers.AxFailure;
	import com.axon.base.managers.AxErrorManager;
	import com.axon.mvc.helpers.AxVerbs;
	import com.axon.mvc.patterns.mediator.AxMediator;
	
	import flash.display.DisplayObject;
	
	import helpers.Constants;
	import helpers.ListaPleitos;
	import helpers.Verbs;
	
	import models.vo.Pleito;
	import models.vo.User;
	
	import mx.containers.TabNavigator;
	
	import views.components.PleitoListaCertificacaoView;
	
	public class PleitoListaCertificacaoMediator extends AxMediator
	{
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function PleitoListaCertificacaoMediator( viewComponent: DisplayObject ) { super(viewComponent); }
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function get view(): PleitoListaCertificacaoView { return this.viewComponent as PleitoListaCertificacaoView; }
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		override protected function initialize(): void 
		{
			regBroadcastInterests( [ AxVerbs.REFRESH ] );
			 
			regResponseInterest( Pleito, [AxVerbs.LIST, Verbs.AVANCAR, Verbs.VOLTAR_PARA_ANALISE, Verbs.ARQUIVAR] );
		}	
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function eventViewShow(): void
		{
			view.adgMain.dataProvider = null;
			view.adgMain.setFocus();
			callController( Pleito, AxVerbs.LIST, Constants.MNEMONICO_LISTA_CERTIFICACAO );
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function eventEnableGestures(): void
		{
			view.pbAbrir.enabled = (view.selectedPleito != null);
			
			if ( !view.pbAbrir.enabled )
				return;
			
			if ( AppFacade.getInstance().sessionProxy.user.hasRole(User.ROLE_CERTIFICADOR) ) {
				view.action = Verbs.ANALISAR_POPUP; }
			else {
				view.action = AxVerbs.READ_POPUP;
			}
		}

		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function brcastRefresh(sender: Object, body: Object, extraInfo: Object): void
		{
			AppFacade(facade).pleitoProxy.listaCertificacao.invalidate();

			if  ( (view.parent is TabNavigator) && ((view.parent as TabNavigator).selectedChild == view) ) 
				eventViewShow();
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoListSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
			if ( body == AppFacade(facade).pleitoProxy.listaCertificacao ) {
				view.adgMain.dataProvider = (body as ListaPleitos).modelsCollection;
				view.adgMain.axRecallLastSelection(); }
		}
		
		public function respPleitoListFailure( sender: Object, body: Object, extraInfo: Object ): void
		{
			if ( body == AppFacade(facade).pleitoProxy.listaCertificacao )
				AxErrorManager.showFailure( new AxFailure("Falha no carregamento da lista de pleitos em certificação.") );
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoAvancarSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
			if (!(body is Pleito)) return;
			
			if ( (view.adgMain.selectedItem) && 
				 (Pleito(body).id == view.selectedPleito.id) &&
				 (Pleito(body).status != Constants.STATUS_PLEITO_CERTIFICACAO) ) {

				view.adgMain.axRemoveItem(view.selectedPleito); 
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoVoltarParaAnaliseSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
			if (!(body is Pleito)) return;
			
			if ( (view.adgMain.selectedItem) && 
				 (Pleito(body).id == view.selectedPleito.id) &&
				 (Pleito(body).status != Constants.STATUS_PLEITO_CERTIFICACAO) ) {

				view.adgMain.axRemoveItem(view.selectedPleito); 
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function respPleitoArquivarSuccess( sender: Object, body: Object, extraInfo: Object ): void
		{
			if (!(body is Pleito)) return;
			
			if ( (view.adgMain.selectedItem) && 
				 (Pleito(body).id == view.selectedPleito.id) &&
				 (Pleito(body).status != Constants.STATUS_PLEITO_CERTIFICACAO) ) {

				view.adgMain.axRemoveItem(view.selectedPleito); 
			}
		}
		
	}
}